<template>
  <el-dialog
    title="上传文件"
    :close-on-click-modal="false"
    @close="closeHandle"
    :visible.sync="visible">

    <el-upload
      drag
      :action="url"
      :before-upload="beforeUploadHandle"
      :on-success="successHandle"
      multiple
      :file-list="fileList"
      style="text-align: center;">
      <i class="el-icon-upload"></i>
      <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
    </el-upload>

    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" >
      <el-form-item label="应用名称" prop="appName">
        <el-input v-model="dataForm.appName" disabled="" placeholder="应用名称"></el-input>
      </el-form-item>
      <el-form-item label="应用路径" prop="path">
        <el-input  v-model="dataForm.path" disabled="" placeholder="应用路径"></el-input>
      </el-form-item>
      <el-form-item label="APP版本" prop="version">
        <el-input v-model="dataForm.version" placeholder="APP版本"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>


    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        // url: '',
        num: 0,
        successNum: 0,
        fileList: [],
        dataForm: {
          appName: '',
          path: '',
          version: '',
          remark: ''
        },
        dataRule: {
          version: [
            {required: true, message: 'APP版本不能为空', trigger: 'blur'}
          ],
          remark: [
            {required: true, message: '备注不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.url = this.$http.adornUrl(`/st/stapp/upload?token=${this.$cookie.get('token')}`)
        this.visible = true
      },

      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/st/stapp/save`),
              method: 'post',
              data: this.$http.adornData({
                'path': this.dataForm.path,
                'version': this.dataForm.version,
                'remark': this.dataForm.remark,
                'appName': this.dataForm.appName,
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$refs['dataForm'].resetFields()
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      // 上传之前
      beforeUploadHandle (file) {
        this.num++
      },
      // 上传成功
      successHandle (response, file, fileList) {
        this.fileList = fileList
        this.successNum++
        if (response && response.code === 0) {
          this.dataForm.appName=response.stApp.appName;
          this.dataForm.path=response.stApp.path;
            // this.$confirm('操作成功, 是否继续操作?', '提示', {
            //   confirmButtonText: '确定',
            //   cancelButtonText: '取消',
            //   type: 'warning'
            // }).catch(() => {
            //   this.visible = false
            // })
        } else {
          this.$message.error(response.msg)
        }
      },
      // 弹窗关闭时
      closeHandle () {
        this.fileList = []
        this.$emit('refreshDataList')
      }
    }
  }
</script>
